
package com.cari.baju.yog;

import android.app.Activity;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.cari.baju.yog.utils.Utils;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks;
import com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener;
import com.google.android.gms.location.LocationClient;

public class PilihJarakActivity extends Activity implements OnClickListener,
        ConnectionCallbacks,
        OnConnectionFailedListener {
    private Bundle data;
    private Button btnCari;
    private EditText etInputJarak;

    private LocationClient locationClient;
    private Location location;
    private boolean locFound = false;

    private TextView title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pilih_jarak);

        title = (TextView) findViewById(R.id.title_judul);
        etInputJarak = (EditText) findViewById(R.id.et_input_jarak);

        // getSupportActionBar().setTitle("Pencarian");

        locationClient = new LocationClient(this, this, this);

        btnCari = (Button) findViewById(R.id.btn_cari);

        title.setTypeface(Utils.getCustomFont(this));

        data = getIntent().getExtras();

        btnCari.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        // getMenuInflater().inflate(R.menu.pilih_jarak, menu);
        return true;
    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        locationClient.connect();
    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        locationClient.disconnect();
    }

    @Override
    public void onClick(View v) {
        if (v == btnCari) {
            if (locFound) {
                if (!TextUtils.isEmpty(etInputJarak.getText().toString())) {
                    data.putInt("jarak", Integer.parseInt(etInputJarak.getText().toString()));
                    data.putDouble("lat", location.getLatitude());
                    data.putDouble("lng", location.getLongitude());
                    startActivity(new Intent(this, MapActivity.class).putExtras(data));
                }
            } else {
                Toast.makeText(this, "Location not found", Toast.LENGTH_SHORT).show();
            }

        }

    }

    @Override
    public void onConnectionFailed(ConnectionResult result) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onConnected(Bundle connectionHint) {
        // TODO Auto-generated method stub
        location = locationClient.getLastLocation();
        if (location != null)
            locFound = true;

    }

    @Override
    public void onDisconnected() {
        // TODO Auto-generated method stub

    }

}
